package com.flyou.qiubai.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.qiubai.Utils.DelHtmlTags;
import com.flyou.qiubai.Utils.DisplayUtil;
import com.flyou.qiubai.Utils.HttpUtils;
import com.flyou.qiubai.Vo.HttpURL;
import com.flyou.qiubai.adapter.MainDataListView;
import com.flyou.qiubai.adapter.MianLeftMenuList;
import com.flyou.qiubai.dialog.Exit_dialog;
import com.flyou.qiubai.dialog.Loading_From_Web;

public class MainActivity extends Activity {
	private ListView MainListView;
	private ListView MainLeftMenuListView;
	private SimpleAdapter mainSimpleAdapter;
	private SlidingLayout slidingLayout;
	private Loading_From_Web dialog;
	private TextView main_text_title;
	private int flag = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
		new MainListView().execute(HttpURL.URL_ARRAY[0] + 1 + ".htm");
		dialog.showDialog();

	}

	// 初始化操作
	private void init() {
		// …………………………………………………一些初始化操作…………………………………………………………………………
		dialog = new Loading_From_Web(MainActivity.this);
		MainLeftMenuListView = (ListView) this
				.findViewById(R.id.main_left_menulist);
		MainListView = (ListView) this.findViewById(R.id.main_listView);
		slidingLayout = (SlidingLayout) findViewById(R.id.slidingLayout);
		main_text_title = (TextView) this.findViewById(R.id.main_text_title);
		
		// 对左侧菜单的listview进行监听
		MainLeftMenuListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dialog.showDialog();
				main_text_title.setText(MianLeftMenuList.MENU_DATA[position]);
				new MainListView().execute(HttpURL.URL_ARRAY[position] + 1
						+ ".htm");

			}
		});

		// 对首页listview选项及你想那个监听
		MainListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(MainActivity.this,
						DataDetailedActivity.class);
				new DisplayUtil();
				intent.putExtra("dataText", HttpUtils.TEXT[position]);
				intent.putExtra("title", HttpUtils.TITLE[position]);
				startActivity(intent);
			}
		});
	}

	// option菜单操作
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	// option菜单监听操作
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.main_menu_share:
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, "Share");
			intent.putExtra(
					Intent.EXTRA_TEXT,
					"哇塞，这个软件好棒啊，有许多笑话可以看啊，快来试试吧！！！(来自：笑话百科)"
							+ "http://flyou.oss-cn-qingdao.aliyuncs.com/%E7%AC%91%E8%AF%9D%E7%99%BE%E7%A7%91.apk?Expires=1418477771&OSSAccessKeyId=h1aPe0ezGvnHaOGG&Signature=ZGPR0Q5%2FWd71WWhVvpmZ46UMlTg%3D");

			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(Intent.createChooser(intent, getTitle()));
			break;
		// 菜单与主界面的切换
		case R.id.main_menu_report:
			if (slidingLayout.isLeftLayoutVisible()) {
				slidingLayout.scrollToRightLayout();
			} else {
				slidingLayout.scrollToLeftLayout();
			}
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	// 获取context上下文菜单
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getMenuInflater().inflate(R.menu.data_menu, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@SuppressWarnings({ "deprecation", "static-access" })
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// 获取点击的listview position
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
				.getMenuInfo();
		int position = menuInfo.position;
		switch (item.getItemId()) {
		// 分享服务，设置分享内容及意图操作
		case R.id.data_menu_share:

			// 设置意图
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, "Share");
			intent.putExtra(
					Intent.EXTRA_TEXT,
					"笑话标题："
							+ HttpUtils.TITLE[position]
							+ "\n内容：\n"
							+ new DelHtmlTags()
									.delHTMLTag(HttpUtils.TEXT[position])
							+ "\n\t(……来自笑话百科)");

			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(Intent.createChooser(intent, getTitle()));

			break;
		// 剪贴板服务，设置剪贴板内容
		case R.id.data_menu_copy:
			ClipboardManager clipboardManager = (ClipboardManager) super
					.getSystemService(Context.CLIPBOARD_SERVICE);
			clipboardManager.setText(DelHtmlTags
					.delHTMLTag(HttpUtils.TEXT[position]) + "\n\t\t(……来自笑话百科)");
			Toast.makeText(MainActivity.this, "内容已复制至剪贴板", Toast.LENGTH_SHORT)
					.show();
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	// 异步处理网络操作与UI的更新
	class MainListView extends AsyncTask<String, SimpleAdapter, SimpleAdapter> {

		@Override
		protected SimpleAdapter doInBackground(String... params) {
			mainSimpleAdapter = new MainDataListView().homepageListView(
					MainActivity.this, params[0]);
			return mainSimpleAdapter;

		}

		@Override
		protected void onPostExecute(SimpleAdapter result) {
			MainListView.setAdapter(result);
			registerForContextMenu(MainListView);
			slidingLayout.setScrollEvent(MainListView);
			MainLeftMenuListView.setAdapter(new MianLeftMenuList()
					.main_Left_MenuListAdapter(MainActivity.this));
			// 默认第一次显示首页，不进行自动切换
			if (flag > 0) {
				if (slidingLayout.isLeftLayoutVisible()) {
					slidingLayout.scrollToRightLayout();
				} else {
					slidingLayout.scrollToLeftLayout();
				}

			}

			dialog.DissDialog();
			flag++;
		}

	}

	@Override
	// 对手机的物理返回键进行监听，复写其中的处理方法
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 处理back返回按键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 退出
			new Exit_dialog().dialog(MainActivity.this);
			// System.exit(0);
			// 表示此按键已处理，不再交给系统处理，
			// 从而避免游戏被切入后台
			return true;
		}
		return super.onKeyDown(keyCode, event);// 不是Back键就交给父类解决

	}
}
